
#include "gmatlab.h"

// GIFunctionInfo *g_irepository_matlab_find_method(GIBaseInfo *type, gchar *name);
void mexFunction(int nlhs, mxArray *lhs[], int nrhs, const mxArray *rhs[])
{
  mxArray *ret;
  GIBaseInfo *type;
  gchar *name;

  // Check and convert arguments
  if (nrhs != 2) g_matlab_error("gmatlab:numArg", "g_irepository_matlab_find_method() takes 2 arguments");
  if (nlhs != 1) g_matlab_error("gmatlab:numRet", "g_irepository_matlab_find_method() has 1 return value");
  if (mxGetNumberOfElements(rhs[0]) != 1) g_matlab_error("gmatlab:badArray", "g_irepository_matlab_find_method() Arg 1 must have one element");
  if (mxGetClassID(rhs[0]) != MX_GPOINTER)
    g_matlab_error("gmatlab:badArray", "g_irepository_matlab_find_method() Arg 1 must be int, not %s", mxGetClassName(rhs[0]));
  type = ((gpointer*)mxGetData(rhs[0]))[0];
  if (mxGetClassID(rhs[1]) != mxCHAR_CLASS)
    g_matlab_error("gmatlab:badType", "g_irepository_matlab_find_method() Arg 2 must be char, not %s", mxGetClassName(rhs[1]));
  name = g_matlab_mxArray_get_utf8(rhs[1]);
  g_matlab_cleanup_add(g_free, name);
  if (type == NULL) g_matlab_error("gmatlab:unknown", "No such method %s.", name);
  
  // Call function
  g_matlab_escape_init();
  lhs[0] = g_irepository_matlab_find_method(type, name);
  
  // Prepare return value

  g_matlab_escape_clear();
  g_matlab_cleanup();
}
